package com.smart4j.chapter.modules.service.impl;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.smart4j.chapter.common.util.DateUtils;
import com.smart4j.chapter.modules.dao.ClientDao;
import com.smart4j.chapter.modules.dao.impl.ClientDaoImpl;
import com.smart4j.chapter.modules.entity.Client;
import com.smart4j.chapter.modules.service.ClientService;

public class ClientServiceImpl implements ClientService<Client> {
	
	private ClientDao dao = new ClientDaoImpl();
	
	@Override
	public List<Client> findList(Client t) {
		List<Client> findList = dao.findList(t);
		return findList;
	}

	@Override
	public Client get(String id) {
		Client client = dao.get(id);
		return client;
	}

	@Override
	public Client get(Client obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean del(String id) {
		int del = dao.del(id);
		return del > 0;
	}

	@Override
	public Boolean del(Client t) {
		
		return false;
	}

	@Override
	public Boolean update(Client client , HttpServletRequest request) {
		client.setUpdateDate(new Date());
		convertValue(client,request);
		int result = dao.update(client);
		return result > 0;
	}
	
	@Override
	public Boolean add(Client t , HttpServletRequest request) {
		convertValues(t, request);
		int result = dao.add(t);
		return result > 0;
	}

	@Override
	public List<Client> findPage(Client t) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * description: 反射实现
	 * @param t
	 * @param request
	 * void 
	 * @version v1.0
	 * @author w
	 * @date 2018年8月9日 下午3:44:14
	 */
	@SuppressWarnings("deprecation")
	private void convertValues(Client t, HttpServletRequest request) {
		Field[] declaredFields = t.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			field.setAccessible(true);
			String value = request.getParameter(field.getName());
			if(null != value){
				try {
					if(field.getType().equals(Integer.class)){
						field.set(t, Integer.valueOf(value));
					}else if(field.getType().equals(Date.class) && field.getName().equals("createDate")){
						t.setCreateDate(value,"yyyy-MM-dd");
					}else if(field.getType().equals(Date.class)){
						field.set(t, com.smart4j.chapter.common.util.DateUtils.parse(value));
					}else {
						field.set(t, value);
					}
				} catch (IllegalArgumentException | IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			field.setAccessible(false);
		}
	}
	
	/**
	 * description: request 对象中的值 转存到 client 对象中
	 * @param t
	 * @param request
	 * void 
	 * @version v1.0
	 * @author w
	 * @date 2018年8月9日 下午3:43:47
	 * @deprecated only support Client object 。
	 */
	@Deprecated
	private void convertValue(Client client, HttpServletRequest request) {
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			switch (name) {
			case "id":
				client.setId(request.getParameter(name));
				break;
			case "name":
				client.setName(request.getParameter(name));
				break;
			case "contact":
				client.setContact(request.getParameter(name));
				break;
			case "phone":
				client.setPhone(request.getParameter(name));
				break;
			case "email":
				client.setEmail(request.getParameter(name));
				break;
			case "remarks":
				client.setRemarks(request.getParameter(name));
				break;
			case "createDate":
				try {
					Field field = client.getClass().getDeclaredField("createDate");
					field.setAccessible(true);
					field.set(client, DateUtils.parse(request.getParameter(name)));
					field.setAccessible(false);
				} catch (NoSuchFieldException | SecurityException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			
		}
	}
	
}
